package controller

import (
	"ollama-go/dto"
	"ollama-go/query"
	"ollama-go/service"

	"github.com/androidsr/sc-go/model"
	"github.com/androidsr/sc-go/sgin"
	"github.com/gin-gonic/gin"
)

func init() {
	sgin.AddRouter(DbDataSourceController{
		dbDataSourceService: service.NewDbDataSourceService(),
	})
}

type DbDataSourceController struct {
	dbDataSourceService *service.DbDataSourceService
}

// 查询
// @Router [get] [/dbdatasource/:id]
func (m DbDataSourceController) Get(c *gin.Context) any {
	id := c.Param("id")
	if id == "" {
		return model.NewFailDefault("ID不能为空")
	}
	return m.dbDataSourceService.Get(id)
}

// 增加
// @Router [post] [/dbdatasource]
func (m DbDataSourceController) Save(c *gin.Context, dto *dto.DbDataSourceDTO) any {
	dto.Id = ""
	return m.dbDataSourceService.Save(dto)
}

// 修改
// @Router [put] [/dbdatasource/edit]
func (m DbDataSourceController) UpdateById(c *gin.Context, dto *dto.DbDataSourceDTO) any {
	if dto.Id == "" {
		return model.NewFailDefault("ID不能为空")
	}
	dto.TenantId = c.GetString(USER)
	return m.dbDataSourceService.UpdateById(dto)

}

// 删除
// @Router [delete] [/dbdatasource/:id]
func (m DbDataSourceController) DeleteById(c *gin.Context) any {
	id := c.Param("id")
	if id == "" {
		return model.NewFailDefault("ID不能为空")
	}
	return m.dbDataSourceService.DeleteById(id)
}

// 查询列表
// @Router [post] [/dbdatasource/page]
func (m DbDataSourceController) QueryPage(c *gin.Context, query *query.DbDataSourceQueryDTO) any {
	return m.dbDataSourceService.QueryPage(query)
}

// 分页下拉
// @Router [post] [/dbdatasource/list]
func (m DbDataSourceController) QuerySelect(c *gin.Context, query *model.SelectQueryDTO) any {
	return m.dbDataSourceService.QueryList(query)
}

/*
// 获取表详情
// @Router [get] [/dbdatasource/getTableDetail]
func (m DbDataSourceController) GetTableDetail(c *gin.Context) any {
	tableNames := c.Query("tableNames")
	createTable, err := m.dbDataSourceService.GetTableDetail(c.Query("dbname"))
	if err != nil {
		return model.NewFailDefault(err.Error())
	}
	return model.NewOK(createTable)
}

// 分页下拉
// @Router [get] [/dbdatasource/getTables]
func (m DbDataSourceController) GetTables(c *gin.Context, query *model.SelectQueryDTO) any {
	tables, err := m.dbDataSourceService.GetTables(c.Query("dbname"))
	if err != nil {
		return model.NewFailDefault(err.Error())
	}
	return model.NewOK(tables)
}
*/
